import { province_list,city_list,county_list } from './area-data'
import { changeUserInfo } from '@/api/userApi.js'

export default {
  data(){
    return{
      area:{
        province:null,
        city:null,
        county:null,
      },
    }
  },
  methods:{
    jiexi(area,tag){
      if(typeof area == 'number') area=String(area)
      const obj = {
        province: area.slice(0,2),
        city: area.slice(0,4),
        county: area
      }
      if(tag) return obj
      const a = province_list.find(e=>e.value == obj.province).name
      const b = city_list.find(e=>e.value == obj.city).name
      const c = county_list.find(e=>e.value == obj.county).name
      return `${a}-${b}-${c}`
    },
    async areachange(){
      if(!this.change.area){
        let { area } = this.info
        if(area != null)
          this.area = this.jiexi(area,true)
        else
          this.area = {
            province:null,
            city:null,
            county:null,
          }
        this.change.area = true
      }else{
        const { county } = this.area
        // const value = dateFormat("YYYY-MM-DD",new Date(y,m-1,d))
        if(county != this.info.area){
          const list = [{key:'area',value:county}]
          await changeUserInfo( {list,iid:this.info.i_id} )
          this.$message.success("修改成功")
          this.CHANGE_INFO(list)
        }
        this.change.area = false
      }
    }
  },
  computed: {
    province() {
      return province_list
    },
    city() {
      const { province } = this.area
      console.log({province})
      return city_list.filter(e=>e.value.slice(0,2) == province)
    },
    county(){
      const { city } = this.area
      console.log({city})
      return county_list.filter(e=>e.value.slice(0,4) == city)
    }
  },
}